package com.lu.spzx.manager.controller;

import com.lu.spzx.manager.service.SysMenuService;
import com.lu.spzx.manager.service.SysUserService;
import com.lu.spzx.manager.service.ValidateCodeService;
import com.lu.spzx.model.dto.system.LoginDto;
import com.lu.spzx.model.entity.system.SysUser;
import com.lu.spzx.model.vo.common.Result;
import com.lu.spzx.model.vo.common.ResultCodeEnum;
import com.lu.spzx.model.vo.system.LoginVo;
import com.lu.spzx.model.vo.system.SysMenuVo;
import com.lu.spzx.model.vo.system.ValidateCodeVo;
import com.lu.spzx.utils.AuthContextUtil;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Tag(name = "用户接口")
@RestController
@RequestMapping(value = "/admin/system/index")
//@CrossOrigin(allowCredentials = "true", originPatterns = "*", allowedHeaders = "*")
public class IndexController {

    @Autowired
    private SysUserService sysUserService;
    @Autowired
    private SysMenuService sysMenuService;
    @Autowired
    private ValidateCodeService validateCodeService;

    // 用户登录
    @Operation(summary = "登录接口")
    @PostMapping(value = "/login")
    public Result<LoginVo> login(@RequestBody LoginDto loginDto) {
        LoginVo loginVo = this.sysUserService.login(loginDto);
        return Result.build(loginVo, ResultCodeEnum.SUCCESS);
    }

    // 图片验证码
    @GetMapping(value = "/generateValidateCode")
    public Result<ValidateCodeVo> generateValidateCode() {
        ValidateCodeVo validateCodeVo = this.validateCodeService.generateValidateCode();
        return Result.build(validateCodeVo, ResultCodeEnum.SUCCESS);
    }

    // 获取用户信息
    @GetMapping(value = "/getUserInfo")
    public Result<SysUser> getUserInfo(@RequestHeader(name = "token") String token) {
//        SysUser sysUser = this.sysUserService.getUserInfo(token);
        SysUser sysUser = AuthContextUtil.get();
        return Result.build(sysUser, ResultCodeEnum.SUCCESS);
    }

    // 退出
    @DeleteMapping(value = "/logout")
    public Result logout(@RequestHeader(value = "token") String token) {
        this.sysUserService.logout(token);
        return Result.build(null, ResultCodeEnum.SUCCESS);
    }

    // 获取用户菜单
    @GetMapping("/menus")
    public Result<List<SysMenuVo>> menus() {
        List<SysMenuVo> sysMenuVoList = this.sysMenuService.findUserMenuList();
        return Result.build(sysMenuVoList, ResultCodeEnum.SUCCESS);
    }
}